<template>
  <div class="container">
    <Content>
      <el-form :inline="true" ref="formRef" :model="form" :rules="rules">
        <el-form-item label="标题" prop="title">
          <el-input v-model="form.title" style="width: 500px"></el-input>
        </el-form-item>
        <el-form-item label="分类" class="category">
          <el-select v-model="form.categoryId" 
            placeholder="请选择"
            filterable
            
            default-first-option
          >
            <el-option
              v-for="item in categories"
              :key="item.id"
              :label="item.name"
              :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>
        

        <el-form-item label="标签" class="tags">
          <el-select
              v-model="form.tags"
              multiple
              filterable
              allow-create
              default-first-option
              @change="handleChange"
              placeholder="请选择文章标签">
              <el-option
                v-for="item in tags"
                :key="item.id"
                :label="item.name"
                :value="item.id">
              </el-option>
            </el-select>
        </el-form-item> 

        <el-form-item >
          <el-button type="primary" @click="showAddImgDialog">添加封面图片</el-button>
        </el-form-item>

      </el-form>

    <TEditor ref="edit" v-model="form.content"></TEditor>
    </Content>
  </div>

  <el-dialog
      class="addDialog"
      title="添加缩略图"
      :visible="addImgdialogVisible.value"
      width="30%"
      :before-close="handleCloseDialog">
      
      <!-- <el-form-item label="上传图片"> -->
      <el-upload
        class="avatar-uploader"
        action="/api/file/upload"
        :auto-upload="true"
        name="file"
        :show-file-list="false"
        :on-success="handleAddSuccess"
        :before-upload="beforeImageUpload">
        <img v-show="imageUrl" :src="imageUrl" class="thumbnail">
        <i v-show="!imageUrl" class="el-icon-plus avatar-uploader-icon"></i>
      </el-upload>
    <!-- </el-form-item> -->
    <span class="upload-tip">点击上传或更换图片</span>

      <span slot="footer" class="dialog-footer">
        <el-button @click="cancelImgDialog">取 消</el-button>
        <el-button type="primary" @click="confirmImg">确 定</el-button>
      </span>
    </el-dialog>
</template>

<script setup lang="ts">
import TEditor from '@/components/TEditor.vue'
import Content from '@/layout/Content.vue'
import { ref, reactive, watch } from 'vue';
const form = reactive({
  title: '',
  category: '',
  tag: [],
  content: ''
})

const categories = reactive([])
const tags = reactive([])

const addImgdialogVisible = ref(false)
console.log(addImgdialogVisible.value);


const rules = {
  formRules: {
    title: { required: true, message: "请输入标题", trigger: "blur" },
  },
}

const handleClick = (e) => {
  console.log(e.target.value);
}

const handleChange = () => {
  form.tagsNew = val.filter(item => typeof item === 'string')
  // form.tags = form.tags.filter(item => typeof item === 'number')
  console.log(val, form.tagsNew);
}

function showAddImgDialog() {
  console.log('showAddImgDialog');
  
  addImgdialogVisible.value = true
console.log(addImgdialogVisible.value);
}

function handleCloseDialog() {
  addImgdialogVisible.value = false
}

function handleAddSuccess() {

}

watch(() => form.content, (newVal) => {
  console.log(newVal);
})
</script>

<style scoped lang="less">
.container {
}
</style>